package cn.com.klun.tjy.service.Impl;

import cn.com.klun.tjy.base.IStep;
import cn.com.klun.tjy.common.factory.SpringBeanFactory;
import cn.com.klun.tjy.dao.entity.TblTaskBasicConfig;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
* @ClassName: ChildJobTimerRunImpl 
* @Description: 执行子任务的执行器实现
* @author chenmin-ds 
* @date 2015年1月21日 上午10:13:45
 */
public class ChildJobTimerRunImpl implements Job{

	private Logger logger = LoggerFactory.getLogger(this.getClass().getSimpleName());
	
	/**
	 * 执行子任务的执行器实现
	 */
	@Override
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void execute(JobExecutionContext context) throws JobExecutionException {
		Object obj = context.getJobDetail().getJobDataMap().get("TblTaskBasicConfig");
		String taskId = "";
		try{
			if(obj != null){
				TblTaskBasicConfig config = (TblTaskBasicConfig) obj;
				taskId = String.valueOf(config.getId());

				if(TaskThreadCache.getTaskCount("TblTaskBasicConfig_" + taskId)){
					IStep istep = (IStep) SpringBeanFactory.getBean(config.getBusinessObjName());
					String className = istep.getClass().getSuperclass().getSimpleName();
					if(!className.equals("AbastractStep")){
						List<Object> list = istep.queryTaskInfo(config);
						if(list != null){
							for(Object temp : list){
								istep.runTask(temp);
							}
							istep.editTaskInfo(list);
						}else{
							logger.info("没有查询到需要执行的记录："+config.getTaskName());
						}
					}else{
						List<Object> list = istep.queryTaskInfo(config);
						istep.runTask(list);
						istep.editTaskInfo(list);
					}
					TaskThreadCache.setTaskCount("TblTaskBasicConfig_" + taskId);
				}else{
					logger.info("任务线程超限，跳过：taskId=" + taskId);
				}
			}
		}catch(Exception e){
			logger.error("任务执行异常：" + taskId,e);
			TaskThreadCache.setTaskCount("TblTaskBasicConfig_" + taskId);
		}
	}

}
